package com.pactera.bledemo.activity

import android.Manifest
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.dylanc.viewbinding.nonreflection.binding
import com.pactera.bledemo.activity.ble.BLEActivity
import com.pactera.bledemo.activity.thirdparty.BleThirdPartyActivity
import com.pactera.bledemo.activity.traditional.TraditionalActivity
import com.pactera.bledemo.base.BaseActivity
import com.pactera.bledemo.databinding.ActivityMainBinding
import com.permissionx.guolindev.PermissionX

class MainActivity : BaseActivity() {
    private val binding by binding(ActivityMainBinding::inflate)//不使用反射
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.apply {
            actionToolbar.tvTitle.text = "蓝牙测试程序"
            actionToolbar.imgBack.visibility = View.INVISIBLE
            actionToolbar.imgBack.setOnClickListener { finish() }
        }
        getPermission();
        binding.apply {
            btnTraditionalBluetooth.setOnClickListener {
                startActivity(Intent(this@MainActivity, TraditionalActivity::class.java))
            }
            btnBLE.setOnClickListener {
                startActivity(Intent(this@MainActivity, BLEActivity::class.java))
            }
            btnBLE2.setOnClickListener {
                startActivity(Intent(this@MainActivity, BleThirdPartyActivity::class.java))
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ble.closeConnect()
    }

    private fun getPermission() {
        PermissionX.init(this)
            .permissions(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
            )
            .onExplainRequestReason { scope, deniedList ->
                val message = "PermissionX需要您同意以下权限才能正常使用"
                scope.showRequestReasonDialog(deniedList, message, "同意", "拒绝")
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    Toast.makeText(this, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this, "您拒绝了如下权限：$deniedList", Toast.LENGTH_SHORT).show()
                }
            }


    }
}